import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> retList = new ArrayList<>();
        if(root == null) {
            return retList;
        }
        Queue<TreeNode> qu = new LinkedList<>();
        qu.offer(root);
        while(!qu.isEmpty()) {
            int size = qu.size();
            List<Integer> list = new ArrayList<>();
            while(size != 0) {
                TreeNode cur = qu.poll();
                list.add(cur.val);
                size--;
                if(cur.left != null) {
                    qu.offer(cur.left);
                }
                if(cur.right != null) {
                    qu.offer(cur.right);
                }
            }
            retList.add(list);
        }

        return retList;
    }
}
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;

    }
}